 
package com.fyzk.net;

import android.util.Log;

import com.fyzk.utils.LogUtil;
import com.wf.domain.UrlDef;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;


public class ToolsAesCrypt {
	public static String Decrypt(String sSrc, String sKey) throws Exception {
        try {
            byte[] raw = sKey.getBytes("GBK");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
            byte[] encrypted1 = hex2byte(sSrc.getBytes());
            try {
                byte[] original = cipher.doFinal(encrypted1);           
                return new String(original);
            } catch (Exception e) {
                return null;
            }
        } catch (Exception ex) {
            return null;
        }
    }
	
	public static String Encrypt(String sSrc, String sKey) throws Exception {

      byte[] raw = sKey.getBytes("GBK");
      SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
      Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
      cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
      
      byte[] encrypted = cipher.doFinal(sSrc.getBytes());
      return byte2hex(encrypted).toLowerCase();
    }
	
	public static byte[] hex2byte(byte[] b) { 
		   if((b.length%2)!=0) 
		          throw new IllegalArgumentException("闀垮害涓嶆槸鍋舵暟"); 
		            byte[] b2 = new byte[b.length/2]; 
		            for (int n = 0; n < b.length; n+=2) { 
		                String item = new String(b,n,2);
		                b2[n/2] = (byte)Integer.parseInt(item,16); 
		            } 
		   return b2; 
		} 
	
	public static String byte2hex(byte[] b) {
		String hs = "";
        String stmp = "";
        for (int n = 0; n < b.length; n++) {
            stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
            if (stmp.length() == 1) {
                hs = hs + "0" + stmp;
            } else {
                hs = hs + stmp;
            }
        }
       // System.out.println(hs);
        return hs.toUpperCase();
	}
	
	
	public static byte[] encrypt2Bytes(byte[] src, String sKey) {
		try{
		      //byte[] raw = sKey.getBytes("GBK");
			  byte[] raw = sKey.getBytes("UTF-8");;
		      SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
		      Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
		      cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
		      
		      byte[] encrypted = cipher.doFinal(src);
		      return encrypted;
			
		}catch (Exception e ){ e.printStackTrace(); return null;}

	}
	
	
	public static byte[] decrypt2Bytes(byte[] src, String sKey) throws Exception {
        try {
 
            byte[] raw = sKey.getBytes("UTF-8");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
            //byte[] encrypted1 = hex2byte(src);
            byte[] encrypted1 = src;
            try {
                byte[] original = cipher.doFinal(encrypted1);     
                return original;
            } catch (Exception e) {
                LogUtil.e(e.getMessage());
                return null;
            }
        } catch (Exception ex) {
            return null;
        }
    }
	
	public static void main(String[] args)  {
		String data = "d8e5999e8287d5c4dbad36d10409d061d92a22d61fe777b5a7afd6d5fff8de456fe0343bb5be656735841bf6365535b3581102d6f0feb093736d618fce4b92ed87fc1d7d26cfee6847db0cba181355c66c8a3381612e3b7a467f5051c8f9b238664dbe3f15a670e01e40f2edcdeef367fe4b41137fa690e1d06956749d86e4611d4bbd5bfddf52e5f07fa1613c510cc55a9a91da7e63f8ff65ccb3513ca6f349143c8762475de798db25404d0af2af81b57c6c904ec5b734f8877123835a417583a24d96477fd8109365af9181e2ff3437eaecee2e529716b4cee1e57004595069757984d8f23e7b2c13f00addf1c923397825a69e25487b047963186a9bd9a5446e27bbfe445b4760d2a04b6e7c118bb8dfb36b7775fe46dc311679b89322ff5e14656ccf8e6662f79995a3ecb61d0c649edbfe26044b94a143908df03a99a2e3e4ac0cab55b48536ea9072b7b460fb728b851fdf0c9fbb7faf0885d06658024b525ced734324ff20409ae5a66792b9fd5e657d6549868ee720da2ce9eb50a33c2c472a22690685c0628d607d11906a4c20e6fb64d588f733bf7942cec029ece596d4abdb5213464932fdbccd13216425aed4e558884712f14e62fa3ab8fcbf466ff46431cf885f403c8d30c42bd3b0";

        try {
            System.out.println(Decrypt(data.substring(1, data.length() - 1),
                    "B4F9CED935M9419D"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

